//
//  HOMNSSetSelectWhereTests.m
//  HigherOrderMessaging
//
//  Created by Ofri Wolfus on 14/10/06.
//  Copyright 2006 Ofri Wolfus. All rights reserved.
//

#import "HOMNSSetSelectWhereTests.h"


@implementation HOMNSSetSelectWhereTests

- (id)init {
	if ((self = [super init])) {
		unsigned int i;
		
		msg1 = [MSG(hasPrefix:@"a") retain];
		msg2 = [MSG(hasPrefix:@"1") retain];
		
		for (i = 1; i < 10000; i++) {
			NSString *str = [NSString stringWithFormat:@"%u", i];
			
			if ([str hasPrefix:@"1"])
				[(NSMutableSet *)largeSetResult addObject:str];
		}
	}
	
	return self;
}

- (void)dealloc {
	[msg1 release]; msg1 = nil;
	[msg2 release]; msg2 = nil;
	[super dealloc];
}

#pragma mark -
#pragma mark Small Set Tests

- (id)testSmallSet {
	return [smallSet selectWhere:MSG(hasPrefix:@"a"), nil];
}

- (id)testSmallSet_CachedMessage {
	return [smallSet selectWhere:msg1, nil];
}

- (id)testSmallSet_NSEnumerator {
	NSMutableSet *r = [NSMutableSet set];
	NSEnumerator *e = [smallSet objectEnumerator];
	NSString *str;
	
	while ((str = [e nextObject])) {
		if ([str hasPrefix:@"a"])
			[r addObject:str];
	}
	
	return r;
}

#pragma mark -
#pragma mark Large Set Tests

- (id)testLargeSet {
	return [largeSet selectWhere:MSG(hasPrefix:@"1"), nil];
}

- (id)testLargeSet_CachedMessage {
	return [largeSet selectWhere:msg2, nil];
}

- (id)testLargeSet_NSEnumerator {
	NSMutableSet *r = [NSMutableSet set];
	NSEnumerator *e = [largeSet objectEnumerator];
	NSString *str;
	
	while ((str = [e nextObject])) {
		if ([str hasPrefix:@"1"])
			[r addObject:str];
	}
	
	return r;
}

#pragma mark -
#pragma mark Tests Verification

- (BOOL)verifyResult:(NSSet *)set ofTest:(SEL)sel {
	NSString *name = NSStringFromSelector(sel);
	
	if ([name hasPrefix:@"testSmallSet"]) {
		DPTestAssert([set count] == 2U, @"The result doesn't contain 2 objects. Result = %@", set);
		DPTestAssert([set containsObject:@"a"], @"The result doesn't contain 'a'. Result = %@", set);
		DPTestAssert([set containsObject:@"abc"], @"The result doesn't contain 'abc'. Result = %@", set);
	}
	
	return [super verifyResult:set ofTest:sel];
}

@end
